Skip to content

feat(storage): add object contexts in Python GCS SDK#17039

Open
nidhiii-27 wants to merge 7 commits into
mainfrom
feat-object-contexts-13325527155543531515
Open

feat(storage): add object contexts in Python GCS SDK#17039
nidhiii-27 wants to merge 7 commits into
mainfrom
feat-object-contexts-13325527155543531515

Conversation

@nidhiii-27
Copy link
Copy Markdown
Contributor

This PR implements the "Object Contexts" feature in the google-cloud-storage Python SDK, ensuring feature parity with the Go and Java SDKs.

Key changes:

  1. Metadata Attachment: Added ObjectCustomContextPayload and ObjectContexts classes to google/cloud/storage/blob.py. These allow users to define custom key-value pairs (contexts) for objects.
  2. Blob Property: Added a contexts property to the Blob class with appropriate getters and setters. Added contexts to _WRITABLE_FIELDS to enable REST API support for patching and updating these contexts.
  3. Advanced Filtering: Updated the list_blobs method in both google/cloud/storage/client.py and google/cloud/storage/bucket.py to include a keyword-only filter_ parameter. This allows server-side filtering of objects based on various attributes, including custom contexts.
  4. gRPC Support: Enhanced google/cloud/storage/_grpc_conversions.py with:
    • blob_to_proto: Support for converting contexts to GCS V2 proto.
    • proto_to_blob: Support for populating Blob.contexts from GCS V2 proto.
    • get_update_mask: Logic for generating gRPC FieldMask for partial context updates (using shallow masks like contexts.custom.<key>).
  5. Testing: Added new unit tests in google/cloud/storage/tests/unit/test_blob.py and google/cloud/storage/tests/unit/test__grpc_conversions.py to verify the new functionality and ensure no regressions (verified with nox -s unit-3.12). Fixed a minor regression in async write tests.

This implementation allows for advanced metadata management and powerful server-side filtering as requested.


PR created automatically by Jules for task 13325527155543531515 started by @nidhiii-27

Implement object contexts feature parity with Go and Java SDKs. Allows users to attach custom key-value metadata payloads to objects and filter by them during list operations.

- Define ObjectCustomContextPayload and ObjectContexts classes in blob.py.
- Add 'contexts' property to Blob class and include it in _WRITABLE_FIELDS.
- Update list_blobs in Client and Bucket to support 'filter_' parameter.
- Implement gRPC conversion logic and update_mask generation in _grpc_conversions.py.
- Add comprehensive unit tests.

Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for object contexts in the Google Cloud Storage Python SDK, adding the ObjectContexts and ObjectCustomContextPayload classes and a contexts property to the Blob class. It also implements server-side filtering for list_blobs via a new filter_ parameter. Feedback focuses on improving the robustness of protobuf conversions by checking for field presence rather than truthiness, ensuring FieldMask generation for contexts replaces the entire map for consistency with other SDK fields, and hardening the custom context setter to handle None values.

Comment thread packages/google-cloud-storage/google/cloud/storage/_grpc_conversions.py Outdated
Comment thread packages/google-cloud-storage/google/cloud/storage/_grpc_conversions.py Outdated
Comment thread packages/google-cloud-storage/google/cloud/storage/blob.py
google-labs-jules Bot and others added 3 commits May 12, 2026 05:53
- Improve gRPC conversion logic: use 'in' operator for field presence check and improve property mapping.
- Refine update_mask generation logic.
- Update tests to match new conversion logic.

Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
- Implement ObjectContexts and ObjectCustomContextPayload.
- Add 'contexts' property to Blob.
- Add 'filter_' parameter to list_blobs.
- Add gRPC conversion for contexts and update_mask generation.
- Fix unit tests to avoid ADC errors.
- Format code with ruff.

Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
@nidhiii-27 nidhiii-27 marked this pull request as ready for review May 12, 2026 17:17
@nidhiii-27 nidhiii-27 requested a review from a team as a code owner May 12, 2026 17:17
@chandra-siri chandra-siri self-assigned this May 14, 2026
custom_contexts = {}
for key, payload in contexts.custom.items():
payload_params = {"value": payload.value}
if payload.create_time is not None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we set create_time and update_time ?

looks like they are output only - http://shortn/_c796zOs1VO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules remove the create_time and update_time

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed create_time and update_time from blob_to_proto as requested.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file will be used grpc ,hence can you write one system test for zonal buckets ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules add a system test in test_zonal.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a system test for object contexts in test_zonal.py.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add system test for

  1. Adding object context while uploading object
  2. list blobs with object context filter
    Refer - https://docs.cloud.google.com/storage/docs/use-object-contexts#filter_objects_by_contexts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules add the required system tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the required system tests for object contexts and list filtering in test_blob.py.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide option to modify object contexts during

rewrite, copy and compose - https://docs.cloud.google.com/storage/docs/use-object-contexts#manage_object_contexts_during_object_operations

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules add this option to the methods above

@@ -1400,6 +1401,9 @@ def list_blobs(
Note ``soft_deleted`` and ``versions`` cannot be set to True simultaneously. See:
https://cloud.google.com/storage/docs/soft-delete

filter_ (str):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules modify the docblock and add the link

@@ -1516,6 +1517,10 @@ def list_blobs(
Note ``soft_deleted`` and ``versions`` cannot be set to True simultaneously. See:
https://cloud.google.com/storage/docs/soft-delete

:type filter_: str
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules modify the docblock and add the link

google-labs-jules Bot and others added 3 commits May 15, 2026 05:47
- Remove create_time and update_time from gRPC conversion.
- Add system tests for Object Contexts in test_blob.py and test_zonal.py.
- Format code with ruff.

Co-authored-by: nidhiii-27 <224584462+nidhiii-27@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants